package com.augurit.gzcgwsp.smartformCsdlwj.service.impl;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springside.modules.orm.Page;
import org.springside.modules.orm.PropertyFilter;

import com.augurit.common.util.PageUtils;
import com.augurit.common.util.lang.StringUtils;
import com.augurit.gzcgwsp.commonex.util.lang.DateUtilsEX;
import com.augurit.gzcgwsp.printDoc.entity.YwPrintDoc;
import com.augurit.gzcgwsp.smartformCsdlwj.common.SmartDao;
import com.augurit.gzcgwsp.smartformCsdlwj.convert.YwSrCsdlwjConvertor;
import com.augurit.gzcgwsp.smartformCsdlwj.dao.SmartFormCsdlwjDao;
import com.augurit.gzcgwsp.smartformCsdlwj.dao.YwSrCsdlwjDao;
import com.augurit.gzcgwsp.smartformCsdlwj.entity.DlwjYq;
import com.augurit.gzcgwsp.smartformCsdlwj.entity.YwSrCsdlwj;
import com.augurit.gzcgwsp.smartformCsdlwj.service.ISmartFormCsdlwjService;
import com.augurit.gzcgwsp.smartformCsdlwj.web.form.CsdlwjForm;
import com.augurit.gzcgwsp.ywcmn.service.IYwComPrintService;
import com.augurit.gzcgwsp.ywcmn.service.IYwComQyxxService;
import com.augurit.wf.bi.web.form.WfBusInstanceTaskForm;
import com.augurit.wf.common.dao.Jbpm4HistProcinstDao;
import com.augurit.wf.common.dao.Jbpm4HistTaskDao;
import com.augurit.wf.common.entity.Jbpm4HistProcinst;
import com.augurit.wf.common.entity.Jbpm4HistTask;
import com.augurit.wf.common.entity.WfBusInstance;
import com.augurit.wf.common.service.impl.WfBusClientServiceImpl;

/**
 * <P>title:道路挖掘serivce实现类</p>																								
 * <P>Description：</p>
 * <p>Copyright: Copyright (c) 2011 augurit</p>
 * <p>Company: 广州奥格智能科技</p>
 * History：
 * @author:zhangjun
 * @date Nov 15, 2011
 * @version 1.0
 */
@Service
@Transactional
public class SmartFormCsdlwjServiceImpl extends WfBusClientServiceImpl<CsdlwjForm>  implements ISmartFormCsdlwjService{
    @Autowired//道路挖掘dao
    private SmartFormCsdlwjDao smartFormCsdlwjDao;
    @Autowired
    private Jbpm4HistTaskDao jbpm4HistTaskDao;
    @Autowired
    private Jbpm4HistProcinstDao jbpm4HistProcinstDao;
    @Autowired
    private YwSrCsdlwjDao ywSrCsdlwjDao;
    @Autowired
    private IYwComPrintService ywComPrintService;
    @Autowired
    private IYwComQyxxService ywComQyxxService;
	@Autowired
	private SmartDao smartDao;

    
    //通过受理号获取案件
    public List<YwSrCsdlwj> getByAcceptNo(String acceptNo)
    {
    	String sql = "from YwSrCsdlwj t where t.acceptNo='" + acceptNo + "'";
    	List<YwSrCsdlwj> entityList= ywSrCsdlwjDao.find(sql);
        return entityList;
    }
    
    public Page<CsdlwjForm> search(Page<CsdlwjForm> page,
            List<PropertyFilter> filters) {
        // TODO Auto-generated method stub
        return null;
    }

    public Page<CsdlwjForm> search(Page<CsdlwjForm> page, CsdlwjForm form) {
        // TODO Auto-generated method stub
        return null;
    }

    public void delete(Long... ids) {
        // TODO Auto-generated method stub
        
    }

    public CsdlwjForm get(Long id) {
        // TODO Auto-generated method stub
        return null;
    }

    public void save(CsdlwjForm... forms) {
        // TODO Auto-generated method stub
        
    }

    public List getListById(Long id){
        String sql="select * from SR_DLWJSQB_V4 where id="+id;
        return smartFormCsdlwjDao.getListById(sql);
    }

    /*
     * 查询受理日期(non-Javadoc)
     * @see com.augurit.gzcgwsp.smartformCsdlwj.service.ISmartFormCsdlwjService#getSlrq(java.lang.Long, java.lang.String, java.lang.Long)
     */
	public String getSlrq(Long id, String fileCode, Long tempId) {
		String date="";
        List<YwPrintDoc> docs=this.ywComPrintService.findDoc(id,fileCode,tempId);
        if(docs!=null && docs.size()>0){
	            YwPrintDoc doc=docs.get(0);
	            Date createDate=doc.getCreatDate();
	            try {
					date=DateUtilsEX.convertDate2Chinese(createDate);
				} catch (ParseException e) {
					e.printStackTrace();
				}
        }
		return date;
	}

	/*
	 * (non-Javadoc)
	 * @see com.augurit.gzcgwsp.smartformCsdlwj.service.ISmartFormCsdlwjService#insertDays(java.lang.String, java.lang.String, java.lang.String)
	 */
	public void insertDays(String days, String taskInstDbid, String acceptNo) {
		String hql=" from DlwjYq where acceptno='"+acceptNo+"'";
		List list=this.smartFormCsdlwjDao.find(hql);
		
		if(list.size()>0){
			DlwjYq entity=(DlwjYq) list.get(0);
			entity.setYqdays(days);
			this.smartFormCsdlwjDao.save(entity);
		}else{
			DlwjYq entity=new DlwjYq();
			entity.setAcceptno(acceptNo);
			entity.setYqdays(days);
			this.smartFormCsdlwjDao.save(entity);
		}
	}

	public String getYqDays(String taskInstDbid, String acceptNo) {
		String hql=" from DlwjYq where acceptno='"+acceptNo+"'";
		List list=this.smartFormCsdlwjDao.find(hql);
		if(list.size()>0){
			DlwjYq entity=(DlwjYq) list.get(0);
		    return entity.getYqdays();
		}else{
			return "";
		}
		
	}

	/*
	 * (non-Javadoc)
	 * @see com.augurit.gzcgwsp.smartformCsdlwj.service.ISmartFormCsdlwjService#insertOtherInfo(java.util.Map, java.lang.String, java.lang.String)
	 */
	@Override
	public void insertOtherInfo(Map map, String taskInstDbid, String acceptNo) {
		String id=(String) map.get("id");
		String bjgzfcr=(String) map.get("bjgzfcr");
		String bjgzyy=(String) map.get("bjgzyy");
		String bjgzsj=(String) map.get("bjgzsj");
		String bjgzqd=(String) map.get("bjgzqd");
		String bjslsj=(String) map.get("bjslsj");
		String sldwcbrxm=(String) map.get("sldwcbrxm");
		String bjclqd=(String) map.get("bjclqd");
		String bjsljtdd=(String) map.get("bjsljtdd");
		String yqts=(String) map.get("yqts");
		if(StringUtils.isNotBlank(id)){
			YwSrCsdlwj entity =  this.smartDao.get(Long.valueOf(id));
			
			if(StringUtils.isNotBlank(bjgzsj)){
				Date bjgzdate;
				try {
					bjgzdate = DateUtilsEX.dateFormate(bjgzsj, "yyyy年MM月dd日");
					entity.setBjgzsj(bjgzdate);//补交告知时间
				} catch (ParseException e) {
					e.printStackTrace();
				}
			}
			if(StringUtils.isNotBlank(bjslsj)){
				Date bjslsjdate;
				try {
					bjslsjdate = DateUtilsEX.dateFormate(bjslsj, "yyyy年MM月dd日");
					entity.setBjslsj(bjslsjdate);//补交受理时间
				} catch (ParseException e) {
					e.printStackTrace();
				}
			}
			if(StringUtils.isNotBlank(bjgzfcr)){
				entity.setBjgzfcr(bjgzfcr);//补交告知发出人
			}
			if(StringUtils.isNotBlank(bjgzyy)){
				entity.setBjgzyy(bjgzyy);//补交告知发出人
			}
			if(StringUtils.isNotBlank(bjgzqd)){
				entity.setBjgzqd(bjgzqd);//补交告知发出人
			}
			if(StringUtils.isNotBlank(sldwcbrxm)){
				entity.setSldwcbrxm(sldwcbrxm);//补交告知发出人
			}
			if(StringUtils.isNotBlank(bjclqd)){
				entity.setBjclqd(bjclqd);//补交告知发出人
			}
			if(StringUtils.isNotBlank(bjsljtdd)){
				entity.setBjsljtdd(bjsljtdd);//补交告知发出人
			}
			
			this.insertDays(yqts, taskInstDbid, acceptNo);
			this.smartFormCsdlwjDao.save(entity);
		}
	}

	@Override
	public void save(CsdlwjForm form) {
		 if (form == null) return;
		 YwSrCsdlwj entity = null;
	        if(form != null && form.getId() != null){
	        	entity=smartFormCsdlwjDao.findEntity(form.getId());
	            if (StringUtils.isBlank(entity.getAcceptNo()) || form.getAcceptNo().indexOf("XXXX") > 0) form.setAcceptNo(null);
	        }else{
	            entity = new YwSrCsdlwj();
	        }
	        entity.setSqdw(form.getJsdw());
	        //保存建设单位信息
//	    
//	        if(form.getQybh()==null){
//	            YwComQyxxForm qyxxForm = new YwComQyxxForm();
//	        	 if(StringUtils.isNotBlank(form.getSqdw())){
//		            	YwComQyxxForm ywComQyxxForm = ywComQyxxService.getFormBySqdw(form.getSqdw(),"csdlwjqx");
//		            	if(ywComQyxxForm != null){
//		            		qyxxForm = ywComQyxxForm;
//		            	}
//		            }
//	        	   qyxxForm.setTemplateCode("csdlwjqx");
//	        	   qyxxForm.setSqdw(form.getSqdw());
//	        	   qyxxForm.setAddress(form.getJsdwAddress());
//	        	   qyxxForm.setQyphone(form.getJsdwTel());
//	        	   qyxxForm.setFddbr(form.getFddbr());
//	        	   qyxxForm.setQyfzr(form.getLxr());
//	        	   ywComQyxxService.save(qyxxForm);
//	        }else{
//	               YwComQyxxForm qyxxForm = ywComQyxxService.get(Long.valueOf(form.getQybh()));
//	        	   qyxxForm.setTemplateCode("csdlwjqx");
//	        	   qyxxForm.setSqdw(form.getSqdw());
//	        	   qyxxForm.setAddress(form.getJsdwAddress());
//	        	   qyxxForm.setQyphone(form.getJsdwTel());
//	        	   qyxxForm.setFddbr(form.getFddbr());
//	        	   qyxxForm.setQyfzr(form.getLxr());
//				   ywComQyxxService.update(qyxxForm);
//	        }
//	        //施工单位保存
//	        if(form.getSgdwId()==null){
//	            YwComQyxxForm qyxxForm = new YwComQyxxForm();
//	        	 if(StringUtils.isNotBlank(form.getSgdw())){
//		            	YwComQyxxForm ywComQyxxForm = ywComQyxxService.getFormBySqdw(form.getSgdw(),"csdlwjqx_sg");
//		            	if(ywComQyxxForm != null){
//		            		qyxxForm = ywComQyxxForm;
//		            	}
//		            }
//	        	   qyxxForm.setTemplateCode("csdlwjqx_sg");
//	        	   qyxxForm.setSqdw(form.getSgdw());
//	        	   qyxxForm.setAddress(form.getSgdwAddress());
//	        	   qyxxForm.setQyphone(form.getSgdwTel());
//	        	   qyxxForm.setFddbr(form.getAqfzr());
//	        	   qyxxForm.setQyfzr(form.getSgdwLxr());
//	        	   ywComQyxxService.save(qyxxForm);
//	        }else{
//	            YwComQyxxForm qyxxForm = ywComQyxxService.get(form.getSgdwId());
//	        	   qyxxForm.setTemplateCode("csdlwjqx_sg");
//	        	   qyxxForm.setSqdw(form.getSgdw());
//	        	   qyxxForm.setAddress(form.getSgdwAddress());
//	        	   qyxxForm.setQyphone(form.getSgdwTel());
//	        	   qyxxForm.setQyphone(form.getSgdwTel());
//	        	   qyxxForm.setFddbr(form.getAqfzr());
//	        	   qyxxForm.setQyfzr(form.getSgdwLxr());
//				   ywComQyxxService.update(qyxxForm);
//	        }
	        
	        YwSrCsdlwjConvertor.convertFormToVo(form, entity);
	        smartFormCsdlwjDao.save(entity);
	        form.setId(entity.getId());
	}

	@Override
	public Page<CsdlwjForm> searchInputData(Page<CsdlwjForm> page,
			CsdlwjForm form,String area) {
		Page pg = PageUtils.newPageInstance(page);
		StringBuffer hql = new StringBuffer("select wj from YwSrCsdlwj wj , WfBusInstance inst where 1=1 " +
				" and wj.id=inst.masterEntityKey  and inst.masterEntity='YwSrCsdlwj' and inst.busMemo2='"+area+"'");
		List values = new ArrayList();
		if(form != null){
			if(StringUtils.isNotBlank(form.getJsdw())){
				hql.append(" and wj.jsdw like '%"+form.getJsdw()+"%'");
			}
			if(StringUtils.isNotBlank(form.getAcceptNo())){
				hql.append(" and wj.acceptNo like '%"+form.getAcceptNo()+"%'");
			}
		}
		hql.append(" order by wj.id desc");
        smartDao.findPage(pg, hql.toString(), values);		
		List<CsdlwjForm> list = YwSrCsdlwjConvertor.convertVoListToFormList(pg.getResult());
		PageUtils.copy(pg, list, page);
		return page;
	}

	@Override
	public Long getTaskDbIdById(Long id) {
		String hql=" select task from Jbpm4HistTask task,WfBusInstance inst,Jbpm4HistProcinst proc where proc.procInstId=task.procInstId" +
				" and proc.procInstId=inst.procInstId and inst.masterEntity='YwSrCsdlwj' and inst.masterEntityKey="+id;
		
		List list=this.jbpm4HistTaskDao.find(hql);
		if(list.size()>0){
			Jbpm4HistTask task=(Jbpm4HistTask) list.get(0);
			return task.getHistTaskInstDbid();
		}
		return null;
	}
    
	
	private Jbpm4HistProcinst getProcinstById(Long id){
		String hql=" select proc from Jbpm4HistTask task,WfBusInstance inst,Jbpm4HistProcinst proc where proc.procInstId=task.procInstId" +
		" and proc.procInstId=inst.procInstId and inst.masterEntity='YwSrCsdlwj' and inst.masterEntityKey="+id;

		List list=this.jbpm4HistTaskDao.find(hql);
		if(list.size()>0){
			Jbpm4HistProcinst proc=(Jbpm4HistProcinst) list.get(0);
			return proc;
		}
		return null;
	}
	private WfBusInstance getInstById(Long id){
		String hql=" select inst from Jbpm4HistTask task,WfBusInstance inst,Jbpm4HistProcinst proc where proc.procInstId=task.procInstId" +
		" and proc.procInstId=inst.procInstId and inst.masterEntity='YwSrCsdlwj' and inst.masterEntityKey="+id;

		List list=this.jbpm4HistTaskDao.find(hql);
		if(list.size()>0){
			WfBusInstance inst=(WfBusInstance) list.get(0);
			return inst;
		}
		return null;
	}

	
	@Override
	public void updateJbpmState(CsdlwjForm form,WfBusInstanceTaskForm task,String acceptNo) {
	      //改为已结案件
        if(form.getId()!=null){
        	Jbpm4HistProcinst pro=this.getProcinstById(form.getId());
        	if(!"ended".equals(pro.getState())){
        		pro.setState("ended");
            	jbpm4HistProcinstDao.save(pro);
        	}
        
        }
        //手工更新受理号
        if(form.getId()!=null){
        	YwSrCsdlwj entity=ywSrCsdlwjDao.get(form.getId());
        	if(entity!=null){
        		entity.setAcceptNo(acceptNo);
        		ywSrCsdlwjDao.save(entity);
        		String hql=" from WfBusInstance where masterEntityKey='"+form.getId()+"'"
        				+" and masterEntity='YwSrCsdlwj'";
        		Session session=this.ywSrCsdlwjDao.getSession();
        		Query query=session.createQuery(hql);
        		List list=query.list();
        		if(list!=null && list.size()>0){
        			WfBusInstance ins=(WfBusInstance) list.get(0);
        			ins.setBusMemo3(acceptNo);
        			session.update(ins);
        		}
        	}
        }
		
	}

	@Override
	public void deleteMore(String ids) {
		String id[]=ids.split(",");
		for(String dlwjid:id){
			Long wjId=Long.valueOf(dlwjid);
			YwSrCsdlwj dlwj=smartFormCsdlwjDao.findEntity(wjId);
			WfBusInstance inst=this.getInstById(wjId);
			Session session=smartFormCsdlwjDao.getSession();
			session.delete(dlwj);
			session.delete(inst);
		}
		
	}
}

